<template>
    <div class="kfOrderInfo">
        <van-nav-bar title="订单信息首页" left-text="返回" left-arrow border fixed @click-left="$router.go(-1)" />
        <div style="height:50px"></div>
        <!-- <div class="date-box">
            <van-cell title="时间" :value="date" @click="show = true" />
            <div class="line">
                <van-calendar v-model="show" type="range" @confirm="onConfirm" />
            </div>
            <img src="../../../assets/icon/time1.png" alt="">
        </div>
        <div style="height: 50px;background: #fff;"></div> -->
        <div class="date-box">
            <div class="time-text">时间</div>
            <div class="line">
                <div class="border-line" v-if="!timeField1">
                    <van-field readonly clickable :value="`请选择`" @click="timePopupShow1 = true; " right-icon="clock-o" />
                </div>
                <div class="border-line" v-if="timeField1">
                    <van-field readonly clickable :value="$formatDateTime(timeVal1, 'yyyy-MM-dd')" @click="timePopupShow1 = true" right-icon="clock-o" />
                </div>
                <div class="border-line" v-if="!timeField2">
                    <van-field readonly clickable :value="`请选择`" @click="timePopupShow2 = true;" right-icon="clock-o" />
                </div>
                <div class="border-line" v-if="timeField2">
                    <van-field readonly clickable :value="$formatDateTime(timeVal2, 'yyyy-MM-dd')" @click="timePopupShow2 = true" right-icon="clock-o" />
                </div>
            </div>
        </div>
        <van-popup v-model="timePopupShow1" position="bottom">
            <van-datetime-picker v-model="timeVal1" type="date" @cancel="timePopupShow1 = false" @confirm="timeConfirm1" />
        </van-popup>
        <van-popup v-model="timePopupShow2" position="bottom">
            <van-datetime-picker v-model="timeVal2" type="date" @cancel="timePopupShow2 = false" @confirm="timeConfirm2" />
        </van-popup>
        <div class="card-list">
            <van-pull-refresh v-model="isLoading" @refresh="onRefresh">

                <van-list v-model="loading" :finished="finished" finished-text="没有更多了" @load="onLoad">
                    <div v-for="(item,index) in listSource" :key="index" class="card">
                        <div class="card-top" :style="{backgroundImage: 'url(' + imgUrl1 + ')' }">
                            <div class="card-title">
                                <div>{{item.company_name}}</div>
                            </div>
                            <div class="single-line">订单编号：{{item.order_no}}</div>
                            <div class="single-line">订单时间：{{item.create_time}}</div>
                            <div class="single-line">产品名称：{{item.goods_name}}</div>
                            <div class="mtext-line">费用总金额：{{item.order_price}}</div>
                            <div class="mtext-line">是否开具发票：{{item.is_invoice==1?'是':'否'}}</div>
                        </div>
                        <div class="card-btn" :style="{backgroundImage: 'url(' + imgUrl2 + ')' }">
                            <div v-if="item.invoice_state != 0" @click="confirmBt('1',index)" :class="item.invoice_state == 2 ?'gray-color':''">{{item.invoice_state == 1 ?
                                '确认开票':'已开票'}}
                            </div>
                            <div v-if="item.receive_state != 0" @click="confirmBt('2',index)" :class="item.receive_state == 2 ?'gray-color':''">{{item.receive_state == 1 ?
                                '确认收款':'已收款'}}
                            </div>
                            <div v-if="item.finance_order_text!= ''" class="gray-color">审核中</div>
                            <div v-if="item.lay_pay_state == 1" @click="confirmBt('3',index)">后付款</div>
                        </div>
                    </div>
                </van-list>
            </van-pull-refresh>
        </div>

        <div class="pop-window" v-if="isPop">
            <div class="pop-box">
                <img src="../../../assets/icon/del_btn.png" alt="">
                <div class="tishi-text">您确认要删除吗？</div>
                <div class="confirm-btn">确定</div>
                <img src="../../../assets/icon/close_btn.png" alt="" @click="isPop=false">
            </div>
        </div>
    </div>
</template>

<script>
export default {
    name: "kfOrderInfo",
    data() {
        return {
            timeField1: false,
            timeVal1: new Date(),
            timePopupShow1: false,

            timeField2: false,
            timeVal2: new Date(),
            timePopupShow2: false,

            minDate: new Date(),
            maxDate: new Date(),

            imgUrl1: require('../../../assets/img/card_bg1.png'),
            imgUrl2: require('../../../assets/img/card_bg2.png'),

            listSource: [],
            page: 1,
            limit: 10,
            isLoading: false,

            imgUrl: require('../../../assets/img/card_bg.png'),
            list: [],
            loading: false,
            finished: false,
            date: '',
            show: false,
            isPop: false,
            isConfirm: false,

        };
    },
    methods: {
        goUrl(index) {
            this.$router.push(`/kfOrderUserList?id=${this.listSource[index].id}`)
        },
        timeConfirm1() {
            this.timeField1 = true;
            this.timePopupShow1 = false;
            if (this.timeField1 && this.timeField2) {
                this.onRefresh();
            }

        },
        timeConfirm2() {
            this.timeField2 = true;
            this.timePopupShow2 = false;
            if (this.timeField1 && this.timeField2) {
                this.onRefresh();
            }
        },
        onRefresh() {
            this.page = 1;
            this.onLoad("refresh");
        },
        onLoad(type) {
            let params = {
                limit: this.limit,
                page: this.page,
                wx_member_id: localStorage.getItem('wx_member_id'),
                token: localStorage.getItem('token'),
                member_goods_id: this.$route.query.id,
                month_time_range: '',
            };
            if (this.timeField1 && this.timeField2) {
                let timeVal1 = this.timeVal1
                let timeVal2 = this.timeVal2
                params.month_time_range = `${this.$formatDateTime(this.timeVal1, 'yyyy-MM-dd')}~${this.$formatDateTime(this.timeVal2, 'yyyy-MM-dd')}`
            }
            this.$post("/api/order_detail/finance_list", params)
                .then(res => {
                    if (res.code == 1) {
                        if (type == "refresh") {
                            this.listSource = res.data.dataList;
                            this.isLoading = false;
                            this.finished = false;
                        } else {
                            this.listSource = this.listSource.concat(
                                res.data.dataList
                            );
                        }
                        this.loading = false;
                        this.page++;
                        if (res.data.dataList.length < this.limit) {
                            this.finished = true;
                        }
                    }
                })
                .catch(err => {
                });
        },
        onConfirm(date) {
            const [start, end] = date;
            this.show = false;
            this.date = `${this.formatDate(start)} ~ ${this.formatDate(end)}`;
        },
        confirmBt(type, index) {
            const data = this.listSource[index]

            if (type == 1) {
                if (data.invoice_state == 1) {
                    this.$router.push(`/insuranceInvoice?id=${data.id}&type=set`)
                }
                if (data.invoice_state == 2) {
                    this.$router.push(`/insuranceInvoice?id=${data.id}&type=look`)
                }
            }
            if (type == 2) {
                let id = data.id
                if (data.receive_state == 2) {
                    this.$router.push(`/jrInsuranceMonye?id=${id}&type=look`)
                } else {
                    this.$router.push(`/jrInsuranceMonye?id=${id}&type=set`)
                }
            }
            if (type == 3) {
                this.$Dialog.confirm({
                    title: '温馨提示',
                    message: '确认后付款么？',
                    closeOnPopstate: true,
                })
                    .then(() => {
                        let params = {
                            wx_member_id: localStorage.getItem('wx_member_id'),
                            token: localStorage.getItem('token'),
                            id: data.id,
                        };
                        this.$post("/api/order_detail/lagpay", params)
                            .then(res => {
                                if (res.code == 1) {
                                    this.onRefresh();
                                    this.$Toast(res.msg);
                                } else {
                                    this.$Toast(res.msg);
                                }
                            })
                            .catch(err => {
                            });
                    })
                    .catch(() => {
                    });
            }
        }
    },
    created() {
        // 实例创建完成
        // this.infoData();
    },
    beforeCreate() {
        document.querySelector("body").setAttribute("style", "background-color:#fff");
    }
}
</script>

<style scoped lang='less'>
.kfOrderInfo {
}

.date-box {
    display: flex;
    flex-wrap: nowrap;
    align-items: center;
    padding: 11px;

    > div:nth-of-type(1) {
        width: 70px;
    }
}

.line {
    display: flex;
    align-items: center;

    .border-line {
        border: 1px solid #dcdcdc;
        padding: 0 2px;
        margin: 0 3px;
        border-radius: 3px;
    }

    .time-text {
        flex-wrap: nowrap;
        width: 50px;
        font-size: 15px;
        color: #333333;
    }

    > .money-box {
        position: relative;

        > div {
            flex: 1;
            margin: 3px 6px;
            border: 1px solid #dcdcdc;
            color: #333;
            border-radius: 5px;
        }
    }
}

.card-list {
    padding: 16px;
    padding-top: 0;

    .card {
        /*height: 200px;*/
        position: relative;
        color: #fff;
        margin-bottom: 15px;

        .card-top {
            display: flex;
            flex-wrap: wrap;
            background-size: 100% 100%;
            background-repeat: no-repeat;
            padding: 20px 30px;
            border-radius: 8px 8px 0 0;

            .card-title {
                display: flex;
                justify-content: space-between;
                width: 100%;
                margin-bottom: 13px;

                > div:nth-of-type(1) {
                    font-size: 16px;
                    font-weight: bold;
                    flex: 1;
                    padding-right: 10px;
                }

                > div:nth-of-type(2) {
                    font-size: 12px;
                    padding-left: 10px;
                }
            }

            .mtext-line {
                width: 50%;
                font-size: 13px;
                flex-wrap: wrap;
                overflow: hidden;
                text-overflow: ellipsis;
                white-space: nowrap;
            }

            .single-line {
                display: flex;
                justify-content: space-between;
                width: 100%;
                font-size: 13px;
                overflow: hidden;
                text-overflow: ellipsis;
                white-space: nowrap;

                text-overflow: -o-ellipsis-lastline;
                overflow: hidden;
                text-overflow: ellipsis;
                display: -webkit-box;
                -webkit-line-clamp: 1;
                -webkit-box-orient: vertical;

                margin-bottom: 10px;
            }

            > .mtext-line:not(:last-child) {
                margin-bottom: 10px;
            }
        }

        .card-btn {
            display: flex;
            justify-content: center;
            align-items: center;
            width: 100%;
            height: 60px;
            padding-left: 0;
            bottom: 8px;
            background-size: 100% 100%;
            background-repeat: no-repeat;

            div {
                height: 35px;
                width: 135px;
                margin: 0 8px;
                display: flex;
                align-items: center;
                justify-content: center;
                background: #fff;
                opacity: 0.8;
                color: #366cb3;
                border-radius: 5px;
                font-size: 14px;
            }

            .gray-color {
                background-color: #c8c8c8;
                color: #fff;
            }
        }
    }
}

.pop-window {
    background: rgba(0, 0, 0, 0.6);
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    height: 100vh;
    width: 100vw;
    z-index: 100;

    .pop-box {
        width: 80vw;
        height: 233px;
        margin-left: 10vw;
        background: #fff;
        border-radius: 5px;
        margin-top: 50%;
        display: flex;
        align-items: center;
        flex-direction: column;
        padding-top: 45px;
        position: relative;

        > img:nth-of-type(1) {
            width: 69px;
            height: 69px;
        }

        > img:nth-of-type(2) {
            width: 30px;
            height: 30px;
            position: absolute;
            bottom: -45px;
        }

        .tishi-text {
            font-size: 17px;
            color: #333333;
            padding: 20px 0;
        }

        .confirm-btn {
            width: 150px;
            height: 45px;
            background-image: linear-gradient(-83deg, #e61a1a 0%, #f33e3e 100%),
                linear-gradient(#ff6766, #ff6766);
            background-blend-mode: normal, normal;
            border-radius: 22px;
            display: flex;
            align-items: center;
            justify-content: center;
            color: #fefefe;
            font-size: 16px;
        }
    }
}
</style>